package com.shop.api;

import com.shop.config.annotation.TokenToUser;
import com.shop.entity.User;
import com.shop.service.StatisticsService;
import com.shop.util.Result;
import com.shop.util.ResultGenerator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@RestController
@RequestMapping("/api/statistics")
public class StatisticsApi {

    @Resource
    private StatisticsService statisticsService;

    @GetMapping("/getUserStatistics")
    public Result getUserPay(@RequestParam int month, @TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }
        if (loginUser.getUserType() == 0) {
            Double pay = statisticsService.getUserPay(loginUser.getUserId(), month);
            int time = statisticsService.getUserShopTime(loginUser.getUserId(), month);
            Map map = new HashMap<>();
            map.put("pay", pay);
            map.put("time", time);
            return ResultGenerator.genSuccessResult(map);
        }else{
            Double pay = statisticsService.getOrderEarn(loginUser.getUserId(), month);
            int time = statisticsService.getOrderSum(loginUser.getUserId(), month);
            Map map = new HashMap<>();
            map.put("pay", pay);
            map.put("time", time);
            return ResultGenerator.genSuccessResult(map);
        }
    }

    @GetMapping("/getSlider")
    public Result getSlider() {
        List list = statisticsService.getSlider();
        return ResultGenerator.genSuccessResult(list);
    }


}
